package dtn.supports;

import java.util.ArrayList;
import java.util.List;

import dtn.news.R;

import dtn.models.News;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("HandlerLeak")
public class ListNewsAdapter extends BaseAdapter {

	private ArrayList<News> lstNews;
	Context ctxt;
	LayoutInflater myInflater;

	public ListNewsAdapter(ArrayList<News> lstNews, Context ctxt) {
		this.lstNews = lstNews;
		this.ctxt = ctxt;
		myInflater = (LayoutInflater) ctxt
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {

		return lstNews.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return lstNews.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView = myInflater.inflate(R.layout.row, parent, false);
		}
		NewsHolder holder = new NewsHolder(convertView);
		try {
			holder.populateFrom(lstNews.get(position));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (convertView);
	}

	static class NewsHolder {
		private TextView title = null;
		private TextView content = null;
		private ImageView icon = null;
		private Drawable imageIcon = null;
		private View row = null;
		private static News news = null;
		private UIImageHandle uiImageHandle = null;

		NewsHolder(View row) {
			this.row = row;

			title = (TextView) row.findViewById(R.id.row_title);
			content = (TextView) row.findViewById(R.id.row_content);
			icon = (ImageView) row.findViewById(R.id.row_icon);
			uiImageHandle = new UIImageHandle();
		}

		public void populateFrom(final News news) throws InterruptedException {
			synchronized (news) {
				this.news = news;
				title.setText(news.getTitle());
				content.setText(news.getContent());
				if (!news.getIsLoaded()) {
					Thread thread = new Thread() {
						public void run() {
							imageIcon = news.getImage();
							//Tools.LoadImage(icon, news.getIconUrl());
							Message msg = Message.obtain(uiImageHandle);
							uiImageHandle.sendMessage(msg);
						}
					};
					thread.start();
				} else {
					Tools.LoadImage(icon, news.getImage());
				}
			}
		}

		class UIImageHandle extends Handler {
			public void handleMessage(Message msg) {
				Tools.LoadImage(icon, imageIcon);
				super.handleMessage(msg);
			}
		}
	}
}
